package cn.emall.controller;

import cn.emall.config.JwtProperties;
import cn.emall.pojo.Shipping;
import cn.emall.service.ShippingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import token.common.pojo.UserInfo;
import token.common.util.JwtUtil;
import utils.ResponseBean;
import utils.ResultCode;

import javax.servlet.http.Cookie;
import java.util.Date;
import java.util.List;

/**
 * @Author LeeSIUCHI
 * @Date 2020/9/13
 * @Description
 */
@RestController
@RequestMapping("/shipping")
@Api(value = "ShippingController相关的api")
public class ShippingController {
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private ShippingService shippingService;

    @ApiOperation(value = "新增一个收货地址", notes = "参数是一个收货地址的对象")
    @PostMapping("/addShippingInfo")
    public ResponseBean addShippingInfo(@RequestBody Shipping shipping, @CookieValue(value = "EMALL_TOKEN") Cookie cookie) {
        UserInfo userInfo = getUserInfoByCookie(cookie);
        //找不到则返回错误
        if (userInfo == null) {
            return ResponseBean.failure(ResultCode.ADD_DATA_FAILURE);
        }
        shipping.setUserId(userInfo.getId());
        shipping.setCreateTime(new Date());
        shipping.setUpdateTime(new Date());
        shippingService.save(shipping);
        return ResponseBean.success(ResultCode.ADD_DATA_SUCCESS, shipping);
    }

    @ApiOperation(value = "修改收货地址", notes = "参数是一个收货地址的对象")
    @PostMapping("/updateShippingInfo")
    public ResponseBean updateShippingInfo(@RequestBody Shipping shipping, @CookieValue(value = "EMALL_TOKEN") Cookie cookie) {
        UserInfo userInfo = getUserInfoByCookie(cookie);
        //找不到则返回错误
        if (userInfo == null) {
            return ResponseBean.failure(ResultCode.ADD_DATA_FAILURE);
        }
        shipping.setUserId(userInfo.getId());
        shipping.setUpdateTime(new Date());
        QueryWrapper<Shipping> shippingQueryWrapper = new QueryWrapper<>();
        shippingQueryWrapper.eq("id", shipping.getId());
        Boolean result = shippingService.update(shipping, shippingQueryWrapper);
        if (result) {
            return ResponseBean.success(ResultCode.UPDATE_DATA_SUCCESS, shipping);
        } else {
            return ResponseBean.failure(ResultCode.UPDATE_DATA_FAILURE);
        }
    }

    @ApiOperation(value = "删除一个收货地址", notes = "参数是该收货地址的id")
    @DeleteMapping("/deleteShippingInfo/{id}")
    public ResponseBean deleteShippingInfo(@PathVariable Long id) {
        Boolean result = shippingService.removeById(id);
        if (result) {
            return ResponseBean.success(ResultCode.DELETE_DATA_SUCCESS);
        } else {
            return ResponseBean.failure(ResultCode.DELETE_DATA_FAILURE);
        }
    }

    @ApiOperation(value = "查某个收货地址具体信息", notes = "参数是该收货地址的id")
    @GetMapping("/getOneShippingInfo/{id}")
    public ResponseBean getOneShippingInfo(@PathVariable Long id) {
        Shipping shipping = shippingService.getById(id);
        if (shipping != null) {
            return ResponseBean.success(ResultCode.FIND_DATA_SUCCESS, shipping);
        } else {
            return ResponseBean.failure(ResultCode.FIND_DATA_FAILURE);
        }
    }

    @ApiOperation(value = "查该用户的所有收货地址", notes = "根据cookie中的token的userId")
    @GetMapping("/getAllShippingInfo")
    public ResponseBean getAllShippingInfo(@CookieValue(value = "EMALL_TOKEN") Cookie cookie) {
        UserInfo userInfo = getUserInfoByCookie(cookie);
        //找不到则返回错误
        if (userInfo == null) {
            return ResponseBean.failure(ResultCode.FIND_MANYDATA_FAILURE);
        }
        QueryWrapper<Shipping> shippingQueryWrapper = new QueryWrapper<>();
        shippingQueryWrapper.eq("user_id", userInfo.getId());
        List<Shipping> shippingList = shippingService.list(shippingQueryWrapper);
        if (shippingList.size() != 0) {
            return ResponseBean.success(ResultCode.FIND_MANYDATA_SUCCESS, shippingList);
        } else {
            return ResponseBean.failure(ResultCode.FIND_MANYDATA_FAILURE);
        }
    }

    /**
     * 从cookie获取用户信息
     *
     * @param cookie
     * @return 用户信息UserInfo
     */
    public UserInfo getUserInfoByCookie(@CookieValue(value = "EMALL_TOKEN") Cookie cookie) {
        UserInfo userInfo = null;
        try {
            userInfo = JwtUtil.getInfoFromToken(cookie.getValue(), jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userInfo;
    }
}
